﻿using Love;
using MyGrid.Core;
using MyGrid.Core.HexTypeA.Extensions;
using MyGrid.Core.HexTypeA.Visual;

namespace MyGrid.Test
{
    internal class Example01 : Scene
    {
        private class GameObject
        {
            public MGVector2I PosId;
            public bool IsHitted;

            public GameObject()
            {

            }

            public GameObject(MGVector2I posId, bool isHitted)
            {
                PosId = posId;
                IsHitted = isHitted;
            }
        }

        private List<GameObject> _list;
        private float _unitSize = 30;
        private float _zoom = 1f;
        private MGVector2F _focusPos = new MGVector2F(-200, -200);

        public Example01()
        {
            _list = new List<GameObject>();
            var center = (new MGVector2I(0, 0));

            foreach (var item in center.GetArea(3))
            {
                _list.Add(new GameObject(item, false));
            }
        }

        public override void MouseMoved(float x, float y, float dx, float dy, bool isTouch)
        {
            var mousePos = new MGVector2F(x, y);
            for (int i = 0; i < _list.Count; i++)
            {
                var item = _list[i];
                if (HexTypeAHitTester.IsMouseInGrid(item.PosId, _unitSize, _zoom, _focusPos, mousePos))
                {
                    _list[i].IsHitted = true;
                }
                else
                {
                    _list[i].IsHitted = false;
                }
            }
            base.MouseMoved(x, y, dx, dy, isTouch);
        }

        public override void Draw()
        {
            foreach (var item in _list)
            {
                var i = item.PosId.X;
                var j = item.PosId.Y;

                var pos = HexTypeALocator
                      .GetVisualPosition(new Core.MGVector2I(i, j), _unitSize, _zoom, _focusPos);

                if (item.IsHitted)
                {
                    Graphics.SetColor(Color.Red);
                }
                else
                {
                    Graphics.SetColor(Color.Aqua);
                }

                var vertexs = HexTypeAVisual.GetVertexs(pos, _unitSize * _zoom);

                for (int k = 0; k < vertexs.Length; k++)
                {
                    var k2 = (k + 1) % vertexs.Length;
                    var p1 = vertexs[k];
                    var p2 = vertexs[k2];
                    Graphics.Line([p1.X, p1.Y, p2.X, p2.Y]);
                }

                Graphics.Ellipse(DrawMode.Fill, pos.X, pos.Y, 2, 2);
            }
        }


        static void Main(string[] args)
        {
            Boot.Run(new Example01(), new BootConfig() { WindowX = -1000, WindowY = 100, WindowBorderless = false });
        }
    }
}
